package com.controller;


import com.entity.BGoods;
import com.entity.BType;
import com.entity.table.BGoodsTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.impl.BGoodsServiceImpl;
import com.service.impl.BTypeServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Resource
    BGoodsServiceImpl bGoodsService;
    @Resource
    BTypeServiceImpl  bTypeService;

    @GetMapping(value = "")
    public String index (Model model, @RequestParam(value = "goodName", defaultValue = "") String goodName) {
        try {
            Map<String, List<BGoods>> goodsList = new LinkedHashMap<>();
            QueryWrapper wrapper = QueryWrapper.create().limit(6);
            List<BType> typeList = bTypeService.list(wrapper);
            for (BType bType : typeList) {
                int typeId = bType.getId();
                String typeName =bType.getTypeName();
                wrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.GOOD_NAME.like(goodName)).and(BGoodsTableDef.B_GOODS.TYPE_ID.eq(typeId));
                List<BGoods> goods = bGoodsService.list(wrapper);
                goodsList.put(typeName,goods);
            }
            model.addAttribute("goodType", typeList);
            model.addAttribute("goodsList", goodsList);
        } catch (Exception e){
            e.printStackTrace();
        }
        return "client/index";
    }
    @GetMapping(value = "list")
    public String goodList(ModelMap model,@RequestParam(value = "goodName",defaultValue = "") String goodName,
                           @RequestParam(value = "p",defaultValue = "1" ) int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        try {
            List<BType> typeList= bTypeService.list();
            Page<BGoods> page = new Page<>();
            page.setPageSize(pageSize);
            page.setPageNumber(p);
            QueryWrapper queryWrapper =QueryWrapper.create().where(BGoodsTableDef.B_GOODS.GOOD_NAME.like(goodName));
            Page<BGoods> pageInfo = bGoodsService.getMapper().paginateWithRelations(page,queryWrapper);
            model.addAttribute("page",pageInfo);
            model.addAttribute("goodType",typeList);
            return "client/list";
        } catch (Exception e){
            e.printStackTrace();
            return "client/login";
        }
    }
    @GetMapping(value = "goodDetail")
    public String goodDetail(int id,Model model){
        try {
            List<BType>typeList=bTypeService.list();
            BGoods bGoods = bGoodsService.getById(id);
            model.addAttribute("typeList",typeList);
            model.addAttribute("bGood",bGoods);
            return "client/detail";
        } catch (Exception e){
            e.printStackTrace();
            return "client/login";
        }
    }
}
